package com.ccjq.yaoqing.dao.impl;

import com.ccjq.yaoqing.core.DbConnection;
import com.ccjq.yaoqing.dao.StudentDao;
import com.ccjq.yaoqing.domain.Address;


import java.sql.*;

/**
 * describe:
 *
 * @author xugang
 * @date 2018/12/6 17:08
 */
public class StudentDaoImpl implements StudentDao {

    @Override
    public int save(String userId, String name, String school, String phone, Date createTime, String showTime) {
        int result = 0;
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = "INSERT INTO t_student (user_id, name, school, mobile_phone, create_time, show_time) VALUES (?, ?, ?, ?, ?, ?)";
        try {
            conn = DbConnection.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, userId);
            ps.setString(2, name);
            ps.setString(3, school);
            ps.setString(4, phone);
            ps.setDate(5, createTime);
            ps.setString(6, showTime);
            result = ps.executeUpdate();
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, ps, conn);
        }
        return result;
    }

    @Override
    public Address getTimeAndAddress() {
        Address addr = new Address();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT time, address, phone FROM t_timeaddr";
        try {
            conn = DbConnection.getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                addr.setTime(rs.getString(1));
                addr.setAddress(rs.getString(2));
                addr.setPhone(rs.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(rs, ps, conn);
        }
        return addr;
    }
}
